package com.batchengine.test;

import com.batchengine.task.DefaultTask;
import com.batchengine.task.TaskContext;
import com.batchengine.task.TaskMode;
import static com.batchengine.util.EnvUtil.getBaseFolderLocation;
import com.batchengine.util.LoggingFactory;
import org.apache.log4j.Logger;

import java.io.InputStreamReader;

/**
 * User: sunil.incubator@yahoo.com
 * Date: Sep 14, 2009
 * Time: 3:00:47 PM
 */
public class HelloWorldPythonTask extends DefaultTask {

    private static final Logger logger = LoggingFactory.createLogger();

    public HelloWorldPythonTask() {
        super("Task :: HelloWorldPythonTask");
    }

    protected HelloWorldPythonTask(String name) {
        super(name);
    }

    public static void main(String[] args) {

    }

    @Override
    public void execute(TaskContext context) {

        int ch;
        try {

            Process myProcess = Runtime.getRuntime().exec(String.format("python %s/src/com//bashengine/test/helloworld.py", getBaseFolderLocation()));
            InputStreamReader myIStreamReader = new InputStreamReader(myProcess.getInputStream());
            while ((ch = myIStreamReader.read()) != -1) {
                System.out.print((char) ch);
            }
            logger.debug(String.format("=====>> HelloWorldPythonTask#before sleep <<===== By %s @ %s", Thread.currentThread().getName(), System.currentTimeMillis()));
            try {
                Thread.sleep(10 * 5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            logger.debug(String.format("=====>> HelloWorldPythonTask#after sleep <<===== By %s @ %s", Thread.currentThread().getName(), System.currentTimeMillis()));
        }
        catch (Exception e) {
            System.out.println(e);
        }
    }

    @Override
    public TaskMode getTaskMode() {
        return null;
    }
}
